package com.jeesite.modules.wx.web;


import com.alibaba.fastjson.JSONObject;
import com.jeesite.modules.common.BusinessUtils;
import com.jeesite.modules.common.PostUtils;
import com.jeesite.modules.wx.entity.WxActivity;
import com.jeesite.modules.wx.service.WxActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/wx")
@Slf4j
public class WxConfigController {

    /**
     */
    private static String AppID = "wxc86ac9d5537decf6";
    private static String AppSecret = "55ef33c909863b6fb1a1d27adca79f66";
    private static String redirectUrl = "http://api.wangjianxin.top/wx/redirect";

    @Autowired
    private WxActivityService wxActivityService;

    /**
     * 查询wxjs配置信息
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/queryWxConfig")
    @ResponseBody
    public JSONObject queryWxConfig(String wxOpenId, String activityId, String inviteUserOpenId, String type,
                                    @RequestParam(value = "businessId",required = false) String businessId){
        log.info("type===={}",type);
        JSONObject jsonObject = new JSONObject();
        String access_token = BusinessUtils.getAccessToken(AppID,AppSecret);

        String ticket = "";
        if(StringUtils.isEmpty(ticket)){
            String getJsapiticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+access_token+"&type=jsapi";
            String getJsapiticketResult = PostUtils.getToUrl(getJsapiticket);
            JSONObject paredResut = (JSONObject) JSONObject.parse(getJsapiticketResult);
            log.info("queryWxConfig getJsapiticketResult = {}", getJsapiticketResult);
            ticket = (String) paredResut.get("ticket");

        }

        log.info("queryWxConfig ticket = {}", ticket);
        if(null != ticket){
            String noncestr=UUID.randomUUID().toString();
            long timestamp = System.currentTimeMillis()/1000;

            log.info("noncestr:{}",noncestr);
            log.info("timestamp:{}",timestamp);

            String str = "";
            if(type.equals("index")){

                str = "jsapi_ticket="+ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+BusinessUtils.getFrontServer()+"H5index/"+activityId+"/"+wxOpenId+"/"+inviteUserOpenId;

            }else if(type.equals("invite")){
                WxActivity wxActivity = wxActivityService.get(activityId);
                String router = "";
                if(wxActivity.getTheme() == 1){
                    router = "invitation";
                }else if(wxActivity.getTheme() == 2){
                    router = "TInvitation";
                }else if(wxActivity.getTheme() == 3){
                    router = "THInvitation";
                }else if(wxActivity.getTheme() == 4){
                    router = "FInvitation";
                }

                str = "jsapi_ticket="+ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+BusinessUtils.getFrontServer()+router+"/"+activityId+"/"+wxOpenId;

            }else if(type.equals("shop")){
                WxActivity wxActivity = wxActivityService.get(activityId);
                String router = "";
                if(wxActivity.getTheme() == 1){
                    router = "invitation";
                }else if(wxActivity.getTheme() == 2){
                    router = "THshop";
                }else if(wxActivity.getTheme() == 3){
                    router = "THshop";
                }else if(wxActivity.getTheme() == 4){
                    router = "Fshop";
                }

                str = "jsapi_ticket="+ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+BusinessUtils.getFrontServer()+router+"/"+businessId+"/"+activityId;
            }

            log.info("string1={}",str);
            String signature = DigestUtils.sha1Hex(str);
            log.info("queryWxConfig signature = {}", signature);
            jsonObject.put("timestamp",timestamp);
            jsonObject.put("signature",signature);
            jsonObject.put("noncestr",noncestr);
            jsonObject.put("appid",AppID);
            jsonObject.put("ticket",ticket);
        }

        return jsonObject;
    }


}
